/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import modelo.Login;

/**
 *
 * @author Caps
 */
@Stateless
public class LoginFacade extends AbstractFacade<Login>
{

    @PersistenceContext(unitName = "SagrauPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager()
    {
        return em;
    }

    public LoginFacade()
    {
        super(Login.class);
    }

    public Login getLogin(Login login)
    {
        TypedQuery<Login> q = getEntityManager().createQuery("SELECT l from Login l WHERE l.login =:arg1 and l.senha =:arg2", Login.class);
        q.setParameter("arg1", login.getLogin());
        q.setParameter("arg2", login.getSenha());

        q.setMaxResults(1);

        Login saida = null;

        try
        {
            saida = q.getSingleResult();
        } catch (NoResultException e)
        {
            saida = null;
        }
        //System.out.println("Saida do FACADE "+saida);
        return saida;
    }
    
    public boolean existeUsuario(String usuario)
    {
        TypedQuery<Login> q = getEntityManager().createQuery("SELECT l from Login l WHERE l.login =:arg1", Login.class);
        q.setParameter("arg1", usuario);

        
        return !q.getResultList().isEmpty();         

    }
}
